#pragma once

#include <xml/Stream.h>
#include <physics/Registrator.h>

class b2Joint;
struct b2JointDef;

namespace physics
{

class World;
class Node;

class Joint: public xml::ObjectANSI
{
	friend class UnknownJoint;

protected:
	struct JointDef
	{
		Atom   bodyNameA;
		Atom   bodyNameB;
		Node * bodyA;
		Node * bodyB;
		bool   collideConnected;
		
		JointDef() : bodyA(NULL), bodyB(NULL), collideConnected(true)
		{
		}
	} bodiesInfo;

	World *world;
	b2Joint *b2joint;

	std::auto_ptr <b2JointDef> jointDef;

protected:
	Atom name;

protected:
	virtual bool on_node(xml::Provider *prov, const std::string &name);
	virtual bool on_attribute(xml::Provider *prov, const std::string &name, const std::string &value);

public:
	Joint(World *world);
	virtual ~Joint();

	xml::ObjectANSI* GetLoader() { return this; }

	void CopyFrom(Joint *joint);
	bool Setup();

	Node * GetBodyA();
	Node * GetBodyB();
	const Atom& GetName() const { return name; }
};

}